import traceabilityQueryList from '@/services/traceabilityQuery';
import {
  DeploymentUnitOutlined,
  FireOutlined,
  InboxOutlined,
  SafetyOutlined,
  ShopOutlined,
  ShoppingCartOutlined,
} from '@ant-design/icons';
import { Tabs } from 'antd';
import React, { useEffect, useState } from 'react';
import ExhibitionArea from '../ExhibitionArea';

interface TabsProps {
  searchInfo: string;
}

const tabsInfo = [
  {
    label: '原料采购',
    icon: <DeploymentUnitOutlined />,
    showInfoKey: 'material',
  },
  {
    label: '仓库信息',
    icon: <ShopOutlined />,
    showInfoKey: 'materialWareHouse',
  },
  {
    label: '制作信息',
    icon: <FireOutlined />,
    showInfoKey: 'cleanCutting' && 'boilingCooling',
  },
  {
    label: '质检信息',
    icon: <SafetyOutlined />,
    showInfoKey: 'qualityTest',
  },
  {
    label: '物流信息',
    icon: <ShoppingCartOutlined />,
    showInfoKey: 'qualityTest',
  },
  {
    label: '包装信息',
    icon: <InboxOutlined />,
    showInfoKey: 'qualityTest',
  },
];

const App: React.FC<TabsProps> = ({ searchInfo }) => {
  const [traceabilityQueryData, setTraceabilityQueryData] = useState<any[]>([]);

  useEffect(() => {
    const fetchData = async () => {
      try {
        console.log(searchInfo);
        if (searchInfo !== '') {
          const traceabilityQueryRes = await traceabilityQueryList(searchInfo);
          if (traceabilityQueryRes) {
            setTraceabilityQueryData(traceabilityQueryRes);
            console.log(traceabilityQueryRes.data.traceabilityQueryData);
          }
        }
      } catch (error) {
        console.error('Error fetching:', error);
      }
    };
    fetchData();
  }, [searchInfo]);
  console.log(traceabilityQueryData);

  if (traceabilityQueryData.length !== 0) {
    return (
      <Tabs
        defaultActiveKey="1"
        items={tabsInfo.map((data, i) => {
          const id = String(i + 1);
          console.log(traceabilityQueryData);

          return {
            key: id,
            label: data.label,
            children: (
              <ExhibitionArea
                showInfoKey={data.showInfoKey}
                data={traceabilityQueryData}
              />
            ),
            icon: data.icon,
          };
        })}
      />
    );
  }
};

export default App;
